package com.example.testapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.testapplication.adapter.DividerListItem;
import com.example.testapplication.adapter.MyRecyclerViewAdapter;

import java.util.ArrayList;

public class Recycler1Activity extends ApplicationActivity implements View.OnClickListener {
    private Button btn_add;
    private Button btn_delete;
    private Button btn_list;
    private Button btn_grid;
    private Button btn_flow;
    private RecyclerView recyclerview;
    private TextView textView;

    private ArrayList<String> datas;

    private MyRecyclerViewAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler1);
        initView();  // 初始化

        // 设置数据集合
        initData();
        //  设置RecyClerView 的适配器
        adapter = new MyRecyclerViewAdapter(Recycler1Activity.this,datas);
        recyclerview.setAdapter(adapter);
        // LayoutManager  第一参数 上下文  第二个 方向 （横 竖） 第三 是否倒序
        recyclerview.setLayoutManager(new LinearLayoutManager(Recycler1Activity.this,LinearLayoutManager.VERTICAL,false));
        // 滚动到指定位置
       //recyclerview.scrollToPosition(datas.size() -1);

        // 添加分隔线
        /**
         *  设置分隔线
         * */
        recyclerview.addItemDecoration(new DividerListItem(Recycler1Activity.this,DividerListItem.VERTICAL_LIST));

        // 设置点击事件
        adapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, String data) {
                Toast.makeText(Recycler1Activity.this, "data==" + data, Toast.LENGTH_SHORT).show();
            }
        });

        // 设置动画效果
        recyclerview.setItemAnimator(new DefaultItemAnimator());



    }

    private void initData() {
        datas = new ArrayList<>();
        for(int i =0;i<100;i++){
            datas.add("Content_" + i);
        }
    }

    private void initView() {
        btn_add = (Button) findViewById(R.id.btn_add);
        btn_delete = (Button) findViewById(R.id.btn_delete);
        btn_list = (Button) findViewById(R.id.btn_list);
        btn_grid = (Button) findViewById(R.id.btn_grid);
        btn_flow = (Button) findViewById(R.id.btn_flow);
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        textView = (TextView) findViewById(R.id.tv_title);
        textView.setText("Recycler案例");

        // 设置点击事件
        btn_add.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_list.setOnClickListener(this);
        btn_grid.setOnClickListener(this);
        btn_flow.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_add:
                // 添加数据
                adapter.addData(0,"New_Content");
                recyclerview.scrollToPosition(0);
                break;
            case R.id.btn_delete:
                // 删除数据
                adapter.removeData(0);
                break;
            case R.id.btn_list:
                // 设置List类型效果
                recyclerview.setLayoutManager(new LinearLayoutManager(Recycler1Activity.this,LinearLayoutManager.VERTICAL,false));
                break;
            case R.id.btn_grid:
                // 设置Grid类型效果
                // 上下文 列数  方向，顺序
                recyclerview.setLayoutManager(new GridLayoutManager(Recycler1Activity.this,2,GridLayoutManager.VERTICAL,false));
                break;
            case R.id.btn_flow:
                // 设置瀑布流效果
                recyclerview.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
                break;
        }

    }
}
